package one;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;

import java.lang.reflect.Type;

public class CustomJsonSerializer implements JsonSerializer<Boolean> {

    @Override
    public JsonElement serialize(Boolean aBoolean, Type type, JsonSerializationContext jsonSerializationContext) {
        if (aBoolean) {
            return new JsonPrimitive(1);
        }
        return new JsonPrimitive(0);
    }

    public static void main(String[] args) {
        Gson gson = new GsonBuilder().registerTypeAdapter(Boolean.class, new CustomJsonSerializer()).create();

        Door open = new Door("open", false);
        System.out.println(gson.toJson(open));


        Door close = new Door("close", true);
        System.out.println(gson.toJson(close));

    }
}
